define([ "app", "jquery", "md5", "ecb","artDialog","iframeTools", "ajaxfileupload", "services/UserService","services/TipsService",'directives/ImageCrop',
		"directives/AudioPlayer",
		"resources/company/CompanyResource", 
		"resources/UserResource", "resources/ShareResource" ],
		function(app) {
			var deps = [ "$scope", "$state","$sce", "$timeout", "$compile","$window", "$location", "$stateParams","TipsService", "UserService", "UserResource", "ShareResource","CompanyResource" ];

			function controller($scope, $state,$sce,$timeout,$compile, $window, $location, $stateParams,TipsService, UserService, UserResource, ShareResource,CompanyResource) {
				$scope.mapUrl = $sce.trustAsResourceUrl("/recruit/h5/company/map.html"); //URL 为全链接（$sce.trustAsResourceUrl("http://" + url)）
				$scope.companyVo={};
				var user =UserService.getCurrentUser();
				$scope.mobile=user.mobile;
				
				$scope.count=0;
				$scope.addLable =function (){
					
					if($scope.count>=8){
						art.dialog.alert("最多允许添加8个标签");
						return;
					}
					var text =$("#lableText").val();
					if(text==null || text==""){
						art.dialog.alert("标签不能为空！");
						return;
					}
					
					if(text.length>6){
						art.dialog.alert("长度不能超过6个字！");
						return;
					}
					
					var ids =RndNum(4);
					var input ="<div style=\"color: #ffffff\"  data-ng-click=\"removeLabel($event,\'"+text+"\',\'"+ids+"\')\" id=\""+ids+"\"  style=\"display: inline;\"><div class=\"lable_leai\" style=\"text-align: center;margin-top: 6px;padding-top: 5px;float: left;\" >"+text+"</div><div style=\"float: left;\"><img src=\"images/company/delete_leida.png\"  width=\"32px\" height=\"32px\" style=\"margin-top: 6px;cursor: pointer;\"></div></div>";
					
					
					
					var labelStr =$scope.companyVo.labelStringList;
					if(labelStr===undefined){
						labelStr="";
						$scope.companyVo.labelStringList=text+"@"+ids;
						
					}else{
						
						var array =labelStr.split(",");
						for(var i=0;i<array.length;i++){
							if(array[i].indexOf(text+"@")!=-1){
								art.dialog.alert("标签名重复！");
								return;
							}
						}
						$scope.companyVo.labelStringList=labelStr+","+text+"@"+ids;
					}
					var $html = $compile(input)($scope);  
					$("#addLables").append($html)
					$("#lableText").val("");
					$scope.count=$scope.count+1;
					
				}
				
				//产生随机数函数 
				function RndNum(n){
				    var rnd="";
				    for(var i=0;i<n;i++)
				        rnd+=Math.floor(Math.random()*10);
				    return rnd;
				}
				
				$scope.back =function(){
					$state.go('home.accountManagement');
				};
				
				
				/**
				 * 上传营业执照
				 */
				$scope.uploadPic =function (type){
					if(type=='1'){
						$("#chooseFile1").trigger("click"); 
					}
					
					if(type=='2'){
						$("#chooseFile2").trigger("click"); 
					}
					
					if(type=='3'){
						$("#chooseFile3").trigger("click"); 
					}
					
					if(type=='4'){
						$("#chooseFile4").trigger("click"); 
					}
				}
				
				// 删除标签
				$scope.removeLabel =function($event,text,ids){
					 //alert(text);
					 //$($event.target).remove();
					 //$($event.target).nextAll().remove();
					 $("#"+ids).remove();
					 //删除拼接的值
					 var labelStr =$scope.companyVo.labelStringList;
					 var temp =labelStr=labelStr.replace(text+"@"+ids, "");
					 $scope.companyVo.labelStringList=temp;
					 $scope.count=$scope.count-1;
				};
				
			
				
				
				$scope.auth =function(){
					
					if($scope.companyVo.companyName ==null || $scope.companyVo.companyName ==""){
						
						art.dialog.alert("企业名称不能为空");
						return;
					}
					if($scope.companyVo.companyTelephone ==null || $scope.companyVo.companyTelephone ==""){
						art.dialog.alert("企业电话不能为空");
						return;
					}
					if($scope.companyVo.companyDesc ==null || $scope.companyVo.companyDesc ==""){
						art.dialog.alert("企业简介不能为空");
						return;
					}
					
					if($scope.companyVo.yyzzUrl ==null || $scope.companyVo.yyzzUrl ==""){
						art.dialog.alert("营业执照不能为空");
						return;
					}
					if($scope.companyVo.identificationCardfront ==null || $scope.companyVo.identificationCardfront ==""){
						art.dialog.alert("请上传身份证正面");
						return;
					}
					
					if($scope.companyVo.identificationCardback ==null || $scope.companyVo.identificationCardback ==""){
						art.dialog.alert("请上传身份证背面");
						return;
					}
					
					// 验证必填项
					if($scope.companyVo.logoUrl ==null || $scope.companyVo.logoUrl ==""){
						art.dialog.alert("企业头像不能为空");
						return;
					}
					
					var obj =$window.frames['ifamename'].getAddr();
					var json =JSON.stringify(obj);
					var jsonObj =JSON.parse(json)
					var obj =$scope.companyVo;
					if(obj==null){
						obj={};
					}
					
					obj.address =jsonObj.address;
					obj.longitude =jsonObj.lin;
					obj.latitude =jsonObj.lat;
					
					if(jsonObj.address ==null || jsonObj.address ==""){
						art.dialog.alert("请设置企业地址");
						return;
					}
					CompanyResource.auth(obj).success(function(data){
		        		// 返回成功
		        		art.dialog.alert("认证企业成功！");
		        		$state.go('home.accountManagement');
		            });
				};
				
				$scope.change =function(fileObj,type){
					$.ajaxFileUpload({
						url : "restful/company/uploadFile?type="+type,
						secureuri : false,// 一般设置为false
						fileElementId : "chooseFile"+type,// 文件上传表单的id <input type="file" id="fileUpload" name="file" />
						dataType : 'json',// 返回值类型 一般设置为json
						type: 'post',
						data: {'type': type},
						success : function(data) // 服务器成功响应处理函数
						{
							
							if(type=='1'){
								$scope.companyVo.yyzzUrl=data.obj;
								$("#yyzzId").hide();
								$("#yyzz").show();
								$("#yyzz").attr('src',"http://image.radzp.com/"+data.obj); 
							}
							
							if(type=='2'){
								$scope.companyVo.identificationCardfront=data.obj;
								$("#identificationCardfrontId").hide();
								$("#identificationCardfront").show();
								$("#identificationCardfront").attr('src',"http://image.radzp.com/"+data.obj); 
							}
							
							if(type=='3'){
								$scope.companyVo.identificationCardback=data.obj;
								$("#identificationCardbackId").hide();
								$("#identificationCardback").show();
								$("#identificationCardback").attr('src',"http://image.radzp.com/"+data.obj); 
							}
							
							if(type=='4'){
								
								$scope.user.logoUrl=data.obj;
								$scope.companyVo.logoUrl=data.obj;
								$("#logoUrlId").hide();
								$("#logoUrl").show();
								$("#logoUrl").attr('src',"http://image.radzp.com/"+data.obj); 
							}
						},
						error : function(data)// 服务器响应失败处理函数
						{
							console.log("服务器异常");
						}
					});
					return false;
				}
			}
			controller.$inject = deps;
			return app.lazy.controller("AuthenticationController", controller);
		});
